package com.chen.stopwatch.History;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

import com.chen.stopwatch.R;
import com.chen.stopwatch.utils.Utils;

public class HistoryNavigationView implements ViewFactory {
	protected ImageButton mBackBtn = null;

	protected TextSwitcher mTextSwitcher = null;

	protected Activity mContext = null;

	public HistoryNavigationView(Activity context) {
		mContext = context;
		initViews();
	}

	protected void initViews() {
		if (null == mContext)
			return;

		mBackBtn = (ImageButton) mContext.findViewById(R.id.back);
		if (null != mBackBtn) {
			// mBackBtn.setText(R.string.back);
			mBackBtn.setOnClickListener(mOnClickListener);

		}
		mTextSwitcher = (TextSwitcher) mContext.findViewById(R.id.description_view);
		mTextSwitcher.setFactory(this);

		mTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(
			mContext, android.R.anim.fade_in));

		mTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
			mContext, android.R.anim.fade_out));

		refresh(0);
	}

	OnClickListener mOnClickListener = new OnClickListener() {

		public void onClick(View v) {
			if (null != mBackBtn && v.getId() == mBackBtn.getId()) {
				if (null != mContext)
					mContext.onBackPressed();
			}
		}
	};

	public void refresh(long time) {
		if (null == mTextSwitcher || null == mContext)
			return;
		String text = "";
		// 0 means history parent views.
		if (0 == time) {
			text = mContext.getResources().getString(
				R.string.history_sub_default);
		} else {// other means sub views.
			text = Utils.formatStandardTime(time);
		}
		mTextSwitcher.setText(text);
	}

	public void destory() {
		mContext = null;
		mBackBtn = null;
		mTextSwitcher = null;
	}

	public View makeView() {
		if (null == mContext)
			return null;

		TextView view = new TextView(mContext);
		view.setTextColor(mContext.getResources().getColor(
			R.color.general_text_color));
		view.setGravity(Gravity.CENTER);
		view.setSingleLine();

		return view;
	}
}
